//全局设置axios请求拦截器
axios.interceptors.request.use(
    (config) => {
        config.headers['Authorization'] = 'Bearer' + ' ' + localStorage.getItem('token');
        return config
    }, 
    (error) => {
        return Promise.reject(error)
    }

)
//全局设置axios响应拦截器
axios.interceptors.response.use(
    (res) => {
        
        return res
    }, 
    (error) => {
        if(error.response.status==401){
            alert('登陆过期！请重新登陆')
          location.href='http://localhost:8080/%E7%99%BB%E9%99%86%E9%A1%B5%E9%9D%A2.html'
          localStorage.removeItem('token')
        }else if(error.response.status==404){
            alert('访问路径无效！') 
        }else if(error.response.status==500){
            alert('服务器内部错误！')
        }
        return Promise.reject(error)
    }

)
var searbtn = document.querySelector('.box3 >.sear')

var inputEle = document.querySelector('.box3 >.sou')

searbtn.onclick = () => {
    console.log(777);
   axios.get('/one/search', { params:{searchkey: inputEle.value}  }).then((res) => {
        console.log(res);
        
        render(res.data.list)

    })
}

var tops = document.querySelector('.top')

var downs = document.querySelector('.down')

let page = 1
axios.get('/one/list', { params: { page: 1, count: 2 } }).then((res) => {
    render(res.data.list)
})
tops.onclick = () => {
    if (page > 1) {
        axios.get('/one/list', { params: { page: --page, count: 2 } }).then((res) => {
            render(res.data.list)
        })
    }
}
downs.onclick = () => {
    axios.get('/one/list', { params: { page: ++page, count: 2 } }).then((res) => {
        render(res.data.list)
    })

                              

}

var delet = document.querySelector('tbody')
console.log(delet);
delet.onclick = (e) => {
    if (e.target.tagName == 'SPAN' && e.target.classList.contains('icon-shanchu')) {
        // console.log(e.target.dataset.phone);
        axios.post('/one/delete', { phone: e.target.dataset.phone }).then((res) => {
            // console.log(res.dataset.phone);
            render(res.data.list)
        })

    }


}


function render(data) {
    var str = ''
    str = data.map((item, index) => {
        return `
        <tr>
        <th>${item.email}</th>
        <th>${item.name}</th>
        <th>${item.phone}</th>
        <th>${item.ingroup}</th>
        <th>${item.isadmin ? '是' : '否'}</th>
        <th>${item.status ? '正常' : '异常'}</th>
        <th> <span class="iconfont icon-bianji"></span> &nbsp;
        <span class="iconfont icon-shanchu" data-phone="${item.phone}"></span></th>
    </tr>
        `
    }).join('')
    if (str == '') {
        str = `<td style="color:red ; text-align: center"></td>  
        <td style="color:red ; text-align: center"></td>  
        <td style="color:red ; text-align: center">未查询到数据 </td>  
        `
    } document.querySelector('tbody').innerHTML = str
}



axios.get('/one/list', { params: { page: 1, count: 2 } }).then((res) => {
    render(res.data.list)
    console.log(res.data);

})
// axios.post('/one/login', { phone: '18509382620', pass: '1234' }).then((res) => {

//     console.log(res.data);

// })


